package com.lemon.ui.codemaker;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import org.apache.commons.lang3.StringUtils;

import com.lemon.ui.codemaker.dto.ListItem;

public class ModelSelect extends JDialog {
	
	private static final long serialVersionUID = 6818226548481690499L;
	private String projectPath;
	private String artifactId;
	private String companyName;
	private String selectedValue;
	private JList<String> list;
	
	/**
	 * Create the dialog.
	 */
	public ModelSelect(String projectPath,  String artifactId, String companyName) {
		
		this.projectPath = projectPath;
		this.artifactId = artifactId;
		this.companyName = companyName;
		
		setTitle("选择模型");
		setType(Type.UTILITY);
		setBounds(100, 100, 409, 425);
		getContentPane().setLayout(new BorderLayout());
		{
			JScrollPane scrollPane = new JScrollPane();
			getContentPane().add(scrollPane, BorderLayout.CENTER);
			{
				File[] modals = initModal(projectPath,artifactId,companyName);
				DefaultListModel<String> model = new DefaultListModel<>();
				if(modals == null) {
					return;
				}
				for(File file:modals) {
					model.addElement(file.getName().split("\\.")[0]);
				}
				list = new JList<String>(model);
				list.setCellRenderer(new ListItem());
				list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				list.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent even) {
						if(even.getClickCount() == 2) {
							setSelectedValue(list.getSelectedValue());
							dispose();
						}
					}
				});
				
				scrollPane.setViewportView(list);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						if(StringUtils.isBlank(list.getSelectedValue())) {
							JOptionPane.showMessageDialog(null, "请选择模型", "警告", JOptionPane.WARNING_MESSAGE);
							return;
						}
						setSelectedValue(list.getSelectedValue());
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	public File[] initModal(String projectPath,String artifactId,String companyName) {
		File fileSystem = new File(projectPath+"/"+artifactId+"/"+ artifactId+"-dal/src/main/java/com/"+companyName+"/"+artifactId+"/dal/mybatis/model/");
		File[] modals = fileSystem.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				if(name.endsWith("Example.java")) {
					return false;
				} else {
					return true;
				}
			}
		});
		if(modals == null) {
			JOptionPane.showMessageDialog(this,fileSystem.getAbsolutePath()+"下未模型文件！", "出错了", JOptionPane.ERROR_MESSAGE);
		}
		return modals;
	}

	public String getProjectPath() {
		return projectPath;
	}

	public String getArtifactId() {
		return artifactId;
	}

	public String getCompanyName() {
		return companyName;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

}
